const config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: "0",
    swiperHeight: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          swiperHeight: res.windowHeight + 20
        });
      }
    })
    

    

  },loadOrderInfo:function(e){
      console.log(111);
  },
  //滑动切换
  swiperTab: function (e) {
    var userInfo = getApp().globalData.userInfo;
    var index = e.detail.current;
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    console.log(this.data.currentTab);
    wx.request({
      url: config.requestUrl + '/order/getOrderInfo',
      header: { "token":userInfo.xOpenid},
      data: { "status": e.detail.current },
      success(res) {
        console.log(res.data.data);
        that.setData({ "orders": res.data.data });
      }
    })
  },
  //点击切换
  clickTab: function (e) {
    var userInfo = getApp().globalData.userInfo;
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    wx.request({
      url: config.requestUrl + '/order/getOrderInfo',
      header: { "token": userInfo.xOpenid },
      data: { "status": e.target.dataset.current },
      success(res) {
        console.log(res.data.data);
        that.setData({ "orders": res.data.data });
      }
    })

  },
  findLogistics:function(e){
    var orderAddress = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/logisticsInfo/logisticsInfo?orderId='+orderAddress.orderId+"&logisticsCompany="+orderAddress.logisticsCompany+
      "&logisticsNum="+orderAddress.logisticsNum,
    })
  },
  pay:function(e){
    wx.showLoading({
      title: "支付中",
      mask: true
    });
    
    var that =this;
    var userInfo = getApp().globalData.userInfo;
    var orderId = e.currentTarget.dataset.id;
    wx.request({
      url: config.requestUrl+'/getPayInfo',
      header: { "token": userInfo.xOpenid },
      data:{"orderId":orderId},
      success(res){
        wx.hideLoading();
          var payInfo = res.data.data;
        wx.requestPayment({
          timeStamp: payInfo.timeStamp,
          nonceStr: payInfo.nonceStr,
          package: payInfo.package,
          signType: 'MD5',
          paySign: payInfo.paySign,
          success(res) { 
            wx.showLoading({
              title: '支付确认中',
            })
            setTimeout(function () {
              wx.request({
                url: config.requestUrl + '/getPayStatus',
                data: { "orderId": orderId },
                success(res) {
                  if (res.data.data.status == "1") {
                    wx.request({
                      url: config.requestUrl + '/order/getOrderInfo',
                      header: { "token": userInfo.xOpenid },
                      data: { "status": "1" },
                      success(res) {
                        wx.hideLoading();
                        that.setData({ "orders": res.data.data, "currentTab": "1" });
                      }
                    })
                  }
                }
              })

            }, 3000)
          },
          fail(res) {
            
           }
        })
      }
    })
  },
  cancelOrder:function(e){
    var userInfo = getApp().globalData.userInfo;
    var that =this;
    var orderId = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '温馨提示',
      content: '确定取消订单',
      success(res){
        if(res.confirm){
        wx.request({
          url: config.requestUrl + '/order/cancelOrder',
          header: { "token": userInfo.xOpenid },
          data: { "orderId": orderId },
          success(res) {
            that.setData({
              currentTab: 3
            });
          }
        })}
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = getApp().globalData.userInfo;
    var that = this;
    wx.request({
      url: config.requestUrl + '/order/getOrderInfo',
      header:{"token":userInfo.xOpenid},
      data: { "status": that.data.currentTab },
      success(res) {
        that.setData({ "orders": res.data.data });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})